/**
 * 主页面
 */
layui.use([ 'fsMenu', 'layer', 'fsTab', 'fsCommon', 'fsConfig' ], function() {
	var fsTab = layui.fsTab,
		fsConfig = layui.fsConfig,
		fsCommon = layui.fsCommon,
		fsMenu = layui.fsMenu;

	fsMenu.render();

	//初始化显示菜单
	showMenu($("#fsTopMenu li.layui-this").attr("dataPid"));

	if (window.attachEvent) {
		window.attachEvent("hashchange", hashChanged);
	} else if (window.addEventListener) {
		window.addEventListener("hashchange", hashChanged, false);
	}

	hashChanged();

	function hashChanged() {
		//获取路由信息
		var hash = window.location.hash;
		if (!$.isEmpty(hash) && hash.length > 1) {
			var menuId = hash.substring(1);
			//获取layId
			var dom = $('#fsLeftMenu a[menuId="' + menuId + '"]').parent();
			if (dom.length > 0) {
				fsTab.add(dom);
				fsTab.menuSelectCss(dom.attr("lay-id"));

			}
		}
	}


	$("#fsTopMenu").on("click", "li", function() {
		var dataPid = $(this).attr("dataPid");
		showMenu(dataPid);
	});


	//显示菜单
	function showMenu(dataPid) {
		if (!$.isEmpty(dataPid)) {
			$('#fsLeftMenu>li').hide();
			$('#fsLeftMenu>li[dataPid="' + dataPid + '"]').show();
		}
	}

	//渲染tab
	fsTab.render();

	//新增tab
	function addTab(elem) {
		fsTab.add(elem);
	}

	//手机设备的简单适配
	var treeMobile = $('.site-tree-mobile'),
		shadeMobile = $('.site-mobile-shade')

	treeMobile.on('click', function() {
		$('body').addClass('site-mobile');
	});

	shadeMobile.on('click', function() {
		$('body').removeClass('site-mobile');
	});


	//菜单绑定
	$(".fsSwitchMenu").on("click", function() {
		if ($(this).find("i").attr("data-type") == 0) {
			$(this).find("i").html("&#xe602;").attr("data-type", "1");
		} else {
			$(this).find("i").html("&#xe603;").attr("data-type", "0");
		}
		$(".layui-layout-admin").toggleClass("showMenu");
	});
	// 全屏
	$('.fsSwitchFull').on('click', function() {
		var docElm = document.documentElement;
		//W3C  
		if (docElm.requestFullscreen) {
			docElm.requestFullscreen();
		}
		//FireFox  
		else if (docElm.mozRequestFullScreen) {
			docElm.mozRequestFullScreen();
		}
		//Chrome等  
		else if (docElm.webkitRequestFullScreen) {
			docElm.webkitRequestFullScreen();
		}
		//IE11
		else if (elem.msRequestFullscreen) {
			elem.msRequestFullscreen();
		}
		layer.msg('按Esc即可退出全屏');
	});

	/**
	 * 右边菜单
	 */
	$.contextMenu({
		selector : '.layui-tab-title li',
		callback : function(key, options) {
			var layId = $(this).attr("lay-id");
			switch (key) {
				case "refresh":
					fsTab.refresh(layId);
					break;
				case "close":
					fsTab.del(layId);
					break;
				case "closeOther":
					$(this).parent().children("li").each(function(i, e) {
						if ($(this).find(".layui-tab-close").is(":visible")) {
							var newLayId = $(this).attr("lay-id");
							if (layId != newLayId) {
								fsTab.del(newLayId);
							}
						}
					});
					break;
				case "closeAll":
	
					$(this).parent().children("li").each(function(i, e) {
						if ($(this).find(".layui-tab-close").is(":visible")) {
							var newLayId = $(this).attr("lay-id");
							fsTab.del(newLayId);
						}
					});
					break;
				default:
					break;
			}
		},
		items : {
			"refresh" : {
				name : "刷新此页",
				icon : "fa-refresh"
			},
			"close" : {
				name : "关闭标签",
				icon : "fa-close",
				disabled : function() {
					if ($(this).find(".layui-tab-close").is(":visible")) {
						return false;
					}
					return true;
				}
			},
			"closeOther" : {
				name : "关闭其他",
				icon : "fa-ban"
			},
			"closeAll" : {
				name : "关闭全部",
				icon : "fa-window-close"
			}
		}
	});

});